﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_Tower3._0
{
    internal class MenuSystem
    {
        public struct MenuData
        {
            public string name;
            public Action action;
            public MenuData(string name, Action action)
            {
                this.name = name;
                this.action = action;
            }
        }

        int selectionMax;
        int widthMax, frameWidth;
        int offsetX, offsetY;
        //string[] selections;
        int selection; // 选中的是哪个选项
        //Action[] actions;
        //// 委托的声明
        //// 访问权限 delegate 返回类型 自定义委托名 (参数列表);
        //public delegate int MyDelegate(int a, string b);
        //// 给委托绑定事件
        //MyDelegate myDelegate;
        List<MenuData> menuData;

        public MenuSystem(List<MenuData> menuData, int x, int y)
        {
            this.menuData = menuData;
            selectionMax = menuData.Count;
            offsetX = x;
            offsetY = y;
            widthMax = menuData[0].name.Length;
            for (int i = 0; i < selectionMax; i++)
            {
                if (menuData[i].name.Length > widthMax)
                {
                    widthMax = menuData[i].name.Length;
                }
            }
            frameWidth = widthMax + 10;
            //myDelegate = Test;
        }

        //int Test(int aqs,string sqwdq) { return 0; }

        public void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                UpRoll();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                DownRoll();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Enter))
            {
                Confirm();
            }
        }

        public void Render()
        {
            PrintFrame(selectionMax + 2, frameWidth, offsetX, offsetY);

            PrintSelection();
        }

        void PrintFrame(int row, int col, int x, int y, ConsoleColor color = ConsoleColor.Red, bool clear = true)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    else if (clear)
                    {
                        temp = "  ";
                    }
                    Utility.MyPrint(temp, x + j, y + i, color);
                }
            }
        }

        void PrintSelection()
        {
            int middle = offsetX + frameWidth / 2;
            for (int i = 0; i < selectionMax; i++)
            {
                MenuData data = menuData[i];
                // 将选项居中
                Utility.MyPrint(data.name, middle - data.name.Length , offsetY + 1 + i, selection == i ? ConsoleColor.Red : ConsoleColor.White);
            }
        }

        void UpRoll()
        {
            selection--;
            if (selection < 0)
                selection = selectionMax - 1;
        }

        void DownRoll()
        {
            selection++;
            if (selection >= selectionMax)
                selection = 0;
        }

        void Confirm()
        {
            // 委托与事件
            // delegate event
            // 调用外界方法
            // 将方法作为参数传递进来
            // 方法的类型就是委托
            // 系统自带的无参无返回值委托 Action

            // 委托的执行 ?代表如果委托是空的就不执行了
            menuData[selection].action?.Invoke();
        }
    }
}
